Ext.define('iMedproplus.view.admin.user.UserGrid', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.adminuserusergrid',
    store: 'iMedproplus.store.User',
    requires:[
    'iMedproplus.view.admin.user.PasswordForm'
    ],
    id: 'user-gridpanel',
    rowEditor : null,
    autoScroll: true,
    
    initComponent: function() {
        var me = this;
        me.rowEditor = Ext.create('Ext.grid.plugin.RowEditing', {
            clicksToEdit: 2,
            listeners: {
                canceledit: {
                    fn: me.onCancelEdit,
                    scope: me
                },
                edit: {
                    fn: me.onUpdateEdit,
                    scope: me
                }
            }
        });
        Ext.applyIf(me, {
            columns: [
            {
                xtype: 'gridcolumn',
                dataIndex: 'login',
                text: 'Login',
                //flex: 1,
                editor: {
                    xtype: 'textfield',
                    allowBlank: false
                }
            },
            {
                xtype: 'gridcolumn',
                dataIndex: 'firstName',
                text: 'Imi\u0119',
                //flex: 1,
                editor: {
                    xtype: 'textfield',
                    allowBlank: false
                }
            },
            {
                xtype: 'gridcolumn',
                dataIndex: 'lastName',
                text: 'Nazwisko',
                //flex: 1,
                editor: {
                    xtype: 'textfield',
                    allowBlank: false
                }
            },
            {
                xtype: 'gridcolumn',
                dataIndex: 'email',
                text: 'Email',
                //flex: 1,
                editor: {
                    xtype: 'textfield',
                    allowBlank: false
                }
            },
            {
                xtype: 'datecolumn',
                dataIndex: 'createDate',
                text: 'Data utworzenia'
            //flex: 1
            }
                
            ],
            viewConfig: {
            },
            plugins:  [ me.rowEditor],
            dockedItems: [
            {
                xtype: 'toolbar',
                dock: 'top',
                items: [
                {
                    xtype: 'button',
                    text: 'Dodaj',
                    icon: './resources/icons/add.png',
                    listeners : {
                        click : {
                            fn : me.onUserAddButtonClick,
                            scope: me
                        }
                    }
                },
                {
                    xtype: 'button',
                    text: 'Usuń',
                    icon: './resources/icons/delete.png',
                    listeners: {
                        click: {
                            fn : me.onUserRemoveButtonClick,
                            scope: me
                        }
                    }
                },  
                {
                    xtype: 'button',
                    text: 'Zapisz',
                    icon: './resources/icons/database_save.png',
                    listeners: {
                        click: {
                            fn : me.onUserSaveButtonClick,
                            scope: me
                        }
                    }
                },
                {
                    xtype: 'button',
                    text: 'Anuluj',
                    icon: './resources/icons/cancel.png',
                    listeners: {
                        click: {
                            fn : me.onUserCancelButtonClick,
                            scope: me
                        }
                    }
                },
                {
                    xtype: 'button',
                    text: 'Zmie\u0144 hasło',
                    icon: '',
                    listeners: {
                        click: {
                            fn : me.onUserChangePassword,
                            scope: me
                        }
                    }
                }]
            }
            ],
            selModel: Ext.create('Ext.selection.RowModel', {
                mode: 'SINGLE'
            }),
            listeners: {
                selectionchange: {
                    fn: me.onGridpanelSelectionChange,
                    scope: me
                }
            }
        });

        me.callParent(arguments);
    },
    
    onGridpanelSelectionChange: function(tablepanel, selections, options){
        this.fireEvent('userSelectionChange', selections);
    },
    
    onUserAddButtonClick: function(){
        this.fireEvent('itemAdd', this.rowEditor);
    },
    
    onCancelEdit: function(grid){
        this.fireEvent('itemCancelEdit', grid);
    },
    
    onUpdateEdit: function() {
        this.fireEvent('itemUpdate');
    },
    
    onUserRemoveButtonClick: function(){
        var grid = Ext.getCmp('user-gridpanel');
        this.fireEvent('itemRemove', grid.getSelectionModel());
    },
    
    onUserSaveButtonClick: function(){
        this.fireEvent('itemSave');
    },
    
    onUserCancelButtonClick: function(){
        var grid = Ext.getCmp('user-gridpanel');
        grid.store.load();
    },
    
    onUserChangePassword: function(){
        
        if(Ext.getCmp('user-gridpanel').getSelectionModel().selected.length==1){
            var passWindow = Ext.widget('adminuserpasswordform');
            Ext.App.getController('iMedproplus.controller.admin.User').passwordWinow = passWindow;
            passWindow.show();
        }else{
            Ext.Msg.alert('Uwaga', 'Proszę wybrać użytkownika.');
        }
        
    }

});